package com.wgchao.patientcenter.domain.controller;

import com.wgchao.patientcenter.common.BindingError;
import com.wgchao.patientcenter.common.ResultCommon;
import com.wgchao.patientcenter.domain.service.GrxxService;
import com.wgchao.patientcenter.mapstruct.dto.MdmpGrxxCreateDTO;
import com.wgchao.patientcenter.mapstruct.dto.MdmpGrxxDTO;
import com.wgchao.patientcenter.mapstruct.dto.MdmpGrxxUpdateDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @作者：wgchao
 * @时间：19:05 2019/11/10 0010
 * @用途：
 **/
@RestController
@RequestMapping("/patient/manager")
@Api(tags = "患者信息管理")
public class PatientController {
    @Autowired
    private GrxxService grxxService;

    @PostMapping("/save")
    @ApiOperation(value = "新增患者信息")
    @ApiParam(name = "grxxDTO", value = "患者信息", required = true)
    public ResultCommon<MdmpGrxxDTO> save(@RequestBody @Validated MdmpGrxxCreateDTO grxxCreateDTO,
                                          BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return grxxService.save(grxxCreateDTO);
    }

    @GetMapping("/search/{idcard}")
    @ApiOperation(value = "查询患者基本信息", notes = "通过证件编号查询")
    public ResultCommon<MdmpGrxxDTO> search(@PathVariable String idcard) throws Exception{
        return grxxService.search(idcard);
    }

    @DeleteMapping("/delete/{idcard}")
    @ApiOperation(value = "删除患者基本信息", notes = "通过证件编号")
    public ResultCommon delete(@PathVariable String idcard) throws Exception{
        return grxxService.delete(idcard);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新患者信息")
    @ApiParam(name = "grxxDTO", value = "患者信息", required = true)
    public ResultCommon<MdmpGrxxDTO> update(@RequestBody @Validated MdmpGrxxUpdateDTO grxxUpdateDTO,
                                            BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return grxxService.update(grxxUpdateDTO);
    }

}
